//
//  WZHCountDownButton.swift
//  ShiGuangJiaSwift
//
//  Created by point on 2022/8/18.
//

import Foundation
import UIKit


public class WZHCountDownButton: UIButton {
    
    @IBInspectable  var keyInfo = "WZHCountDownButton" {
        didSet{
            
            /**当设置key值的时候,就开始查找operation ,如果有的话,就需要直接给button 赋值 ,并且button 不能点击,如果没有的话,就正常**/
            self.searchOperation { operation in
                self.Set(operation: operation)
            }
        }
    }
    
    public var countDowningClosure : ( (_ time : TimeInterval)->() )?
    public var countDownFinished : ( ()->() )?
    
    ///找到对应的operation
    public func searchOperation(sucess:((_ operation : WZHCountDownOperation) -> ())? = nil , failed:(()->())? = nil) {
        var operation_btn : WZHCountDownOperation?
        
        for operation in WZHCountDownQueue.shared.operations {
            if let operation = operation as? WZHCountDownOperation  , operation.name == self.keyInfo ,!operation.isCancelled{
                operation_btn = operation
                break //跳出循环
            }
        }
        
        if let operation_btn = operation_btn {//找到了
            sucess?(operation_btn)
        }else{//没有找到
            failed?()
        }
        
    }
    /**开始定时**/
    public func startCountDown(totalTime:TimeInterval){
        //拿到 operation 之后统一做的事情
        self.searchOperation { operation in
            self.Set(operation: operation)
        } failed: {
            //如果没有找到,就重新创建一个
            let operation = WZHCountDownOperation()
            operation.leftTime = totalTime;
            operation.name = self.keyInfo;
            WZHCountDownQueue.shared.addOperation(operation)
            self.Set(operation: operation)
        }

    }
    
    /**根据operation 设置but样式**/
    public func Set(operation:WZHCountDownOperation){
        self.isEnabled = false

        operation.countDowning = { timeInterval in
            if let countDowningClosure = self.countDowningClosure {
                countDowningClosure(timeInterval)
            }else{
                self.countDowning(time: timeInterval)
            }
        }
        operation.countDownFinished = { time in
            if let countDownFinished = self.countDownFinished {
                self.isEnabled = true
                countDownFinished()
            }else{
                self.countDownFinished(time: time)
            }
        };
    }
    
    /**正在倒计时**/
    public func countDowning(time : TimeInterval) {
        self.isEnabled = false
        self.setTitle("\(Int(time))秒后重新获取验证码", for: .disabled)
    }
    /**结束倒计时**/
    public func countDownFinished(time : TimeInterval) {
        self.isEnabled = true
        self.setTitle("重新获取验证码", for: .normal)
    }
    /**正在等待**/
    public func countDownWaiting(){
        self.setTitle("等待中...", for: .disabled)
        self.isEnabled = false
    }
    
    
    public func destroy(){
        
        self.searchOperation { operation in //找到这个 operation
//            WZHCountDownQueue.shared.cancelAllOperations()
            operation.cancel()
            
        }
    }
}
